/* kernel/tty.c 
 * 
 * Copyright (C) 2021, 2022 intirain. 
 *
 * This file is part of evx. 
 * 
 * evx is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * evx is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with evx. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <kernel/init.h>
#include <kernel/module.h>
#include <kernel/kernel.h>
#include <kernel/tty.h>
#include <kernel/mm.h>

#include <sys/types.h>
#include <lib/klist.h>


struct klist_node ttys = KLIST_NODE_INIT(&ttys);

int tty_register(struct tty *tty) {
	struct klist_node *n = kmalloc(sizeof(struct klist_node));

	if (!n) 
		panic("tty_register: Out of memory");

	klist_init(n);

	n->data = tty;
	klist_add(&ttys, n);

	tty_write(tty, tty->dev_name);
	tty_write(tty, "\n\n");

	return 0;
}

int tty_write(struct tty *tty, char *s) {
	int err;

	while (*s) {
		if ((err = tty->putc(*s))) 
			return err;

		s++;
	}

	return 0;
}

int tty_write_all(char *p) {
	struct klist_node *n;
	int err;

	for (n = ttys.next; n != &ttys; n = n->next) 
		if ((err = tty_write((struct tty *) n->data, p))) 
			return err;
 	return 0;
}

